package com.gopay.cron.web.controller;

import com.gopay.common.constants.fund.FundFileSourceConstants;
import com.gopay.common.domain.fund.vo.FileBizParam;
import com.gopay.common.domain.fund.vo.PageData;
import com.gopay.common.util.FastJsonUtil;
import com.gopay.common.util.LogUtil;
import com.gopay.cps.service.FileHandlerService;
import com.gopay.scheduler.service.fund.FundBaseOperateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;


/**
 * 手动执行基金任务，非专业人员禁止操作。
 *
 * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
 * @date 2017-08-11 09:17:50
 */
@Controller
@RequestMapping("/fund")
public class FundCronController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource(name = "fileHandlerService")
    private FileHandlerService fundFileRemoteService;
    @Autowired
    private FundBaseOperateService operateService;

    /**
     * 用于手动执行国付宝转基金转账操作，可选择日期及判断是否校验银行通知，用法如下：
     * http://localhost:8017/cron/fund/fund2GopayTransMoney.shtml?key=121&date=20170810&check=true
     * 其中key为当前日期的平方
     * 日期yyyyMMdd及结算日期
     * check用于判断是否对银行通知的金额及订单金额做校验，可不填，默认为true
     * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
     * @date 2017-08-11 10:02:52
     */
    @RequestMapping(value="/fund2GopayTransMoney",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String index(HttpServletRequest request,HttpServletResponse response) throws Exception {
        PageData pd = new PageData(request);
        LogUtil.info(logger,"页面传入参数为：%s",FastJsonUtil.toJSONString(pd));
        String key = pd.getString("key");
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH);
        String keyCalc = day * day + "";
        if (!key.equals(keyCalc)) {
            return "key不匹配！";
        }
        String settleDay = pd.getString("date");
        if (StringUtils.isBlank(settleDay)){
            return "日期为空，请检查！";
        }
        if (settleDay.length()!=8){
            return "日期格式不正确，请检查！";
        }
        Boolean check = true;
        String checkObj = pd.getString("check");
        if(StringUtils.isNotBlank(checkObj)){
            check = Boolean.parseBoolean(checkObj);
        }
        try {
            LogUtil.info(logger, "【手动执行基金转国付宝】开始，清算日期：%s", settleDay);
            FileBizParam fileBizParam = new FileBizParam(settleDay, operateService.queryAllFundCorp(), FundFileSourceConstants.CORN_HANDLE);
            fileBizParam.setCheckNotify(check);
            LogUtil.info(logger, "【手动执行基金转国付宝】传入指令：%s", FastJsonUtil.toJSONString(fileBizParam));
            String res = fundFileRemoteService.fund2GopayTransferMoney(fileBizParam);
            LogUtil.info(logger, "【手动执行基金转国付宝】结束，返回结果：%s", res);
        } catch (Exception e) {
            LogUtil.error(logger, e, "【手动执行基金转国付宝】异常");
            return "执行失败！"+ e.getMessage();
        }
        return "成功执行！";
    }

}
